﻿//---------------------------------------------------------------------------

#include <vcl.h>
#pragma hdrstop

#include <direct.h>
#include <stdio.h>
#include <string>

#include "mainfrm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "LPComponent"
#pragma link "MLDSPlayer"
#pragma link "SLCommonFilter"
#pragma link "SLControlCollection"
#pragma link "VCL.LPControl"
#pragma link "VLBasicGenericFilter"
#pragma link "VLCommonDisplay"
#pragma link "VLCommonFilter"
#pragma link "VLDSImageDisplay"
#pragma link "VLDSVideoPlayer"
#pragma link "VLGenericFilter"
#pragma link "VLGenericFilter"
#pragma link "VLAVILogger"
#pragma resource "*.dfm"


const UnicodeString	tSavedDir("..\\videos\\saved\\");

THauptformular *Hauptformular;
//---------------------------------------------------------------------------
__fastcall THauptformular::THauptformular(TComponent* Owner)
	: TForm(Owner), bDrawLine(true), bXmlEof(false),bLineDrawable(true)
{
	// videoListBox mit aktuellen Dateien des Video - Verzeichnisses füllen
  	videoListBox->Directory = "..\\..\\videos\\";
//  videoListBox->Directory = "F:\\videos\\";

	// linWidthDisplay mit voreingestellter Linienbreite beschriften
//	lineWidthDisplay->Caption = "Linienbreite: "+ IntToStr(2*(lineWidthTrackBar->Position + 1));
	// onOffButton mit default - Text beschriften
	onOffButton->Caption = "Abseitslinie ausblenden";

	// Buttons zu Beginn ausblenden
	playPauseButton->Enabled = false;

	// Doppelklick - Event auf ein Element der videoListBox initialisieren und
	// mit entsprechendem Verweis auf Callback - Funktion
	videoListBox->OnDblClick = videoListBoxOnDblCLick;

	// Callback für
	videoPlayer->OnStop = videoOnStop;
	videoPlayer->OnReset=videoOnReset;

	videoLogger->Enabled = false;

	this->ptTmpBitmap = new TVLBitmap;

	warningSymbol->Picture->LoadFromFile("..\\bilder\\warnung_bw.jpg");
}
//---------------------------------------------------------------------------


__fastcall THauptformular::~THauptformular()
{
  delete this->ptTmpBitmap;
}

void __fastcall THauptformular::frameFilterProcessData(	TObject *Sender,
													TVLCVideoBuffer InBuffer,
													TVLCVideoBuffer &OutBuffer,
													bool &SendOutputData)
{
	//Aktuelle Bildnummer
	unsigned int uiFNr;

	if(bXmlEof)
		return;


	//Input-Puffer in Bitamp laden.
	InBuffer.ToBitmap(ptTmpBitmap);

	//Tatsächliche Bildnummer berechenen
	uiFNr = this->videoPlayer->CurrentFrame+1;

	if(uiFNr%2==1) {
		uiFNr=uiFNr/2+1;

		try {
			tLine=tXmlParser.getLineData(uiFNr);
			bLineDrawable=true;
		} catch (const ESwenPApp& exc) {

			switch(exc.getType())
			{
				case SWENPAPP_EXC_NO_DATASET:
				warningSymbol->Picture->LoadFromFile("..\\bilder\\warnung.jpg");
				bLineDrawable=false;
				break;

				case SWENPAPP_EXC_EOF:
				bXmlEof=true;
				warningSymbol->Picture->LoadFromFile("..\\bilder\\warnung.jpg");
				break;

				default: break;
			}
			return;
		}
	}


	if(!bDrawLine || !bLineDrawable)
		return;

	warningSymbol->Picture->LoadFromFile("..\\bilder\\warnung_bw.jpg");

	//Linienbreite
	ptTmpBitmap->Canvas->Pen->Width = 2*(lineWidthTrackBar->Position + 1);

	//Linienfarbe abhängig von Spielsituation
	switch(tLine.getStatus())
	{
	//Abseitslinie kann gezeichnet werden.
		case LINE_STATE_DRAWABLE:
			ptTmpBitmap->Canvas->Pen->Color = clGray;
			break;

	//Abseits potentiell.
		case LINE_STATE_OFFSIDE_ACUTE:
			ptTmpBitmap->Canvas->Pen->Color = clYellow;
			break;

	//Abseits findet statt.
		case LINE_STATE_OFFSIDE_SITUATION:
			ptTmpBitmap->Canvas->Pen->Color = clRed;
			break;
		default: break;
	}

	// Linie Zeichnen in Abhänigkeit von drawLine
	ptTmpBitmap->Canvas->MoveTo(tLine.getStartPoint().getX(),
								tLine.getStartPoint().getY());
	ptTmpBitmap->Canvas->LineTo(tLine.getEndPoint().getX(),
								tLine.getEndPoint().getY());

	//Verändertes Bitmap in Output-Puffer schreiben

	OutBuffer.FromBitmap(ptTmpBitmap);

}
//---------------------------------------------------------------------------


void __fastcall THauptformular::frameFilterStart(	TObject *Sender,
													int &AWidth, int &AHeight,
		  											float SampleRate)
{
	doRecord->Enabled=false;
	videoListBox->Enabled=false;

	if(doRecord->Checked)
		playPauseButton->Enabled=false;
}
//---------------------------------------------------------------------------


void __fastcall THauptformular::frameFilterStop(TObject *Sender)
{
	doRecord->Enabled=true;
	videoListBox->Enabled=true;
	playPauseButton->Enabled=true;
}
//---------------------------------------------------------------------------


void __fastcall THauptformular::lineWidthTrackBarChange(TObject *Sender)
{
	// Label unter TrackBar mit aktueller Linienbreite
	// beschriften
	lineWidthDisplay->Caption = "Linienbreite: "+ IntToStr(2*(lineWidthTrackBar->Position + 1));
}
//---------------------------------------------------------------------------


void __fastcall THauptformular::playPauseButtonClick(TObject *Sender)
{

	// Video noch nicht gestartet
	if(!this->videoPlayer->Opened)
	{
		playPauseButton->Caption = "PAUSE";


		tXmlParser.Clear();
		tXmlParser.resetFrmNr();
		bDrawLine=true;
		warningSymbol->Picture->LoadFromFile("..\\bilder\\warnung_bw.jpg");

		playPauseButton->Caption = "PAUSE";

		this->videoPlayer->RestartOnNewFile=true;
		//GetCurrentDir()

		this->videoPlayer->Stop();
		this->videoPlayer->Close();

		//	this->ptVideoPlayer->FileName = a + videoListBox->Items[videoListBox->ItemIndex].GetText();
		this->videoPlayer->FileName = videoListBox->FileName;
		//tXmlParser.LoadFile(videoListBox->FileName.c_str());

		UnicodeString a = "xml";
		UnicodeString b;
		b = videoListBox->FileName.SubString(0,videoListBox->FileName.Length()-3) + a;

		tXmlParser.LoadFile(b.c_str());

		//  VIDEOLISTBOX->FILENAME .AVI ABSCHGNEIDEN UND .XML ANHÄNGEN

		videoListBox->FileName.SubString(0,videoListBox->FileName.Length()-3);


		this->videoPlayer->Open();
		this->videoPlayer->Start();
	}
	// Video ist spielend
	else if(!this->videoPlayer->Paused)
	{
		this->videoPlayer->Pause();
		playPauseButton->Caption = "PLAY";
	}
	// Video ist pausiert
	else
	{
		this->videoPlayer->Resume();
		playPauseButton->Caption = "PAUSE";
	}
}
//---------------------------------------------------------------------------


void __fastcall THauptformular::onOffButtonClick(TObject *Sender)
{
	// drawLine umschalten und Beschriftung des Buttons
	// entsprechend anpassen
    bDrawLine = !bDrawLine;

	if(bDrawLine)
		onOffButton->Caption="Abseitslinie ausblenden";
	else
		onOffButton->Caption="Abseitslinie einblenden";
}
//---------------------------------------------------------------------------



void __fastcall THauptformular::videoListBoxOnDblCLick(TObject *Sender)
{
	// default Werte setzen
	playPauseButton->Enabled = true;
	bXmlEof=false;
	bDrawLine=true;
	bLineDrawable=true;
	warningSymbol->Picture->LoadFromFile("..\\bilder\\warnung_bw.jpg");
	onOffButton->Caption = "Abseitslinie ausblenden";

	playPauseButton->Caption = "PAUSE";

	if(doRecord->Checked) {
		this->videoLogger->Close();
		this->videoLogger->Enabled=false;
	}

	this->videoPlayer->Stop();
	this->videoPlayer->Close();

	// Video wählen
	this->videoPlayer->FileName = videoListBox->FileName;

	UnicodeString a = "xml";
	UnicodeString x = "\\xml";

	// XML wählen und laden
	int videoPosition= videoListBox->FileName.Pos("videos");
	UnicodeString dirToXML;
	dirToXML = videoListBox->FileName.Insert(x,videoPosition+6);
	dirToXML = dirToXML.SubString(0,dirToXML.Length()-3) + a;

	// tXmlParser neu initialisiern
	tXmlParser.Clear();
	tXmlParser.LoadFile(dirToXML.c_str());
	tXmlParser.resetFrmNr();

	this->videoPlayer->Open();
	this->videoPlayer->Start();

	if(doRecord->Checked) {
		videoLogger->FileName = tSavedDir + "output.avi"; //videoListBox->FileName;
		this->videoLogger->Enabled=true;
	 }
}

void __fastcall THauptformular::videoOnStop(TObject *Sender)
{
	if(doRecord->Checked) {
		this->videoLogger->Close();
		this->videoLogger->Enabled=false;
	}
}

void __fastcall THauptformular::videoOnReset(TObject *Sender)
{
	if(doRecord->Checked) {
		this->videoLogger->Close();
		this->videoLogger->Enabled=false;
	}
}






